package com.jiukuaidao.zhulin.util;

import android.text.TextUtils;

import com.jiukuaidao.library.Config;
import com.jiukuaidao.library.net.MyLoggerInterceptor;
import com.jiukuaidao.library.util.LogUtil;
import com.jiukuaidao.library.util.MD5Util;
import com.jiukuaidao.library.util.PackageUtils;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

/**
 * Author : H_Joker
 * Date : 2016/5/26
 * Description :
 */
public class NetUtil {

    private static MyLoggerInterceptor.ResInterListener resHeaderListener;

    /**
     * 获取公参
     *
     * @param url
     * @param headers
     * @param method
     * @param content
     * @return
     */
    public static TreeMap<String, String> getBaseParam(String url, Map<String, List<String>> headers, String method, String content) {

        LogUtil.log("url:" + url);
        LogUtil.log("method:" + method);
        LogUtil.log("content:" + content);

        StringBuilder sb = new StringBuilder();

        sb.append(url.substring(Config.URLConfig.BASE_URL.length()));
        if ("POST".equals(method) || "PUT".equals(method)) {
            if (!TextUtils.isEmpty(content)) {
                sb.append(content);
            }
        }
        sb.append(Config.URLConfig.SIGNKEY);
        LogUtil.log("oriSign:" + sb.toString());
        String sign = MD5Util.getMD5String(sb.toString()).toLowerCase();
        LogUtil.log("sign:" + sign);

        TreeMap<String, String> baseParams = new TreeMap<>();
        baseParams.put("Accept-APIVersion", PackageUtils.getVersionName().trim());//API版本
        baseParams.put("Auth-UID", UserInfoUtil.getUserCode());// 用户标识
        baseParams.put("Req-Client", "Android");// 客户端标识
//        baseParams.put("Req-Client", "Wap");// 客户端标识
        baseParams.put("Auth-Sign", sign);// 签名
        return baseParams;
    }

    /**
     * 获取签名
     *
     * @param params
     * @return
     */
    public static String getApiSign(TreeMap<String, String> params) {
        // 获取字符串参数键值对集合
        StringBuilder sb = new StringBuilder();// 为了签名用的StringBuilder
        for (String name : params.keySet()) {
            sb.append(name);
            sb.append('=');
            sb.append(params.get(name));
            sb.append('&');
        }
        sb.deleteCharAt(sb.length() - 1);// 去掉最后一个&符号
        String str = sb.toString().toLowerCase(Locale.US);// 参数字段拼接

        LogUtil.log("getApiSign()-----", "str--->>" + str);

//        String sign = CreateSignByMD5.createSign(Constants.MD5_KEY, str);// 生成签名的sign
        String sign = "";// 生成签名的sign

        return sign.trim();
    }

    /**
     * 获取默认的接口返回header拦截器
     *
     * @return
     */
    public static MyLoggerInterceptor.ResInterListener getResHeaderListener() {
        if (resHeaderListener == null) {
            synchronized (NetUtil.class) {
                if (resHeaderListener == null) {
                    resHeaderListener = new MyLoggerInterceptor.ResInterListener() {
                        @Override
                        public void getHeader(Map<String, List<String>> headers) {
                            if (headers != null && headers.get("Auth-UID") != null) {
                                String remoteToken = headers.get("Auth-UID").get(0);
                                if (!TextUtils.isEmpty(remoteToken) && !remoteToken.equals(UserInfoUtil.getUserCode())) {
                                    UserInfoUtil.refreshUserCode(remoteToken);
                                }
                            }
                        }
                    };
                }
            }
        }
        return resHeaderListener;
    }

}
